package com.abc.freemarker;

import com.abc.util.FastJsonUtil;
import com.alibaba.fastjson.JSON;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {

   public static void main(String[] args) throws Exception {
//      Set<String> sceneCodes = new HashSet<>();
//      sceneCodes.add("12345gdg");
//      sceneCodes.add("12345g11dg");
//      sceneCodes.add("12345g1dg");
//
//      System.out.println(FastJsonUtil.toJSONString(sceneCodes));

//      String msg = "[]";

//      List msgInfo = JSON.parseArray(msg);

//      System.out.println("msgInfo=" + msgInfo);

//      String template = "你好${姓名!'未知'}，今天是${date?string('yyyy-MM-dd')}";
//
      Map m = new HashMap();
////      m.put("姓名", "管理员");
//      m.put("date", new Date());

      int num = (int) m.get("num");

      System.out.println("num=" + num);
//
//      System.out.println(freemarkerProcess(m, template));
   }





   public static String freemarkerProcess(Map input, String templateStr) {
      StringTemplateLoader stringLoader = new StringTemplateLoader();
      String template = "content";
      stringLoader.putTemplate(template, templateStr);
      Configuration cfg = new Configuration();
      cfg.setTemplateLoader(stringLoader);
      try {
         Template templateCon = cfg.getTemplate(template);
         StringWriter writer = new StringWriter();
         templateCon.process(input, writer);
         return writer.toString();
      }
      catch (IOException e) {
         e.printStackTrace();
      }
      catch (TemplateException e) {
         e.printStackTrace();
      }
      return null;
   }

}